package com.bba.manager.order.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bba.manager.common.model.order.Purchase;
import com.bba.manager.common.model.order.PurchaseDetails;
import com.bba.manager.common.model.order.VO.purchaseQuery;
import com.bba.manager.order.mapper.PurchaseMapper;
import com.bba.manager.order.service.IPurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 采购订单表 服务实现类
 * </p>
 *
 * @author zzy
 * @since 2025-08-12
 */
@Service
public class PurchaseServiceImpl extends ServiceImpl<PurchaseMapper, Purchase> implements IPurchaseService {
    @Autowired
    private PurchaseDetailsServiceImpl purchaseDetailsService;
    @Autowired
    private OpenFeignServiceImpl openFeignService;





    @Override
    public void check(Long purchaseId, boolean flag) {
        if(flag==true){
            //审核通过
            //拿到采购订单详情集合，再依次递减库存
            List<PurchaseDetails> purchaseDetails = purchaseDetailsService.selectByPurchaseId(purchaseId);
            Purchase purchase = this.getById(purchaseId);
            if(purchase.getType()=="1"){
                //汽车
                //递减库存
                purchaseDetails.stream().map(purchaseDetail->{
                    Long goodsNo = purchaseDetail.getGoodsNo();
                    Integer num = purchaseDetail.getQuantity();
                    openFeignService.decountCV(goodsNo,num);
                    return purchaseDetail;
                }
                ).collect(Collectors.toList());
                purchase.setStatus("2");
                this.updateById(purchase);
            }else{
                //配件
                //递减库存
                purchaseDetails.stream().map(purchaseDetail-> {
                    Long goodsNo = purchaseDetail.getGoodsNo();
                    Integer num = purchaseDetail.getQuantity();
                    openFeignService.decountPV(goodsNo, num);
                    return purchaseDetail;
                }).collect(Collectors.toList());
                //修改采购订单状态
                purchase.setStatus("2");
                this.updateById(purchase);
            }
            }else{
            List<PurchaseDetails> purchaseDetails = purchaseDetailsService.selectByPurchaseId(purchaseId);
            Purchase purchase = this.getById(purchaseId);
                //审核不通过
                //删除订单详情
                purchaseDetailsService.deletedByPurchaseId(purchaseId);
            //修改采购订单状态
            purchase.setStatus("3");
            this.updateById(purchase);
                }
            }

    @Override
    public IPage<Purchase> pageALl(purchaseQuery one) {
        IPage<Purchase> page = new Page<>(one.getPage(),one.getLimit());
        LambdaQueryChainWrapper<Purchase> wrapper = new LambdaQueryChainWrapper<>(baseMapper);
        if(one.getPurchaseNo()!=null){
            wrapper.eq(Purchase::getPurchaseNo,one.getPurchaseNo());
        } else if (one.getStatus()!=null&&!one.getStatus().equals("")) {
            wrapper.eq(Purchase::getStatus,one.getStatus());
        } else if (one.getType()!=null&&!one.getType().equals("")) {
            wrapper.eq(Purchase::getType,one.getType());
        }
        return wrapper.page(page);
    }

}

